package com.showde.shoppingmall.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.kim.androidbase.imageloader.ImageLoader;
import com.showde.shoppingmall.R;
import com.showde.shoppingmall.domain.ProductCategory;

public class ProductCategoryAdapter extends BaseAdapter {

	private int[] testData = new int[] { R.drawable.category_nvzhuang,
			R.drawable.category_nanxie, R.drawable.category_xiangbao,
			R.drawable.category_meizhuang, R.drawable.category_nanzhuang,
			R.drawable.category_nanxie, R.drawable.category_peishi,
			R.drawable.category_neiyi, R.drawable.category_shuma,
			R.drawable.category_baihuo };

	private List<ProductCategory> productCategories;

	private ImageLoader imageLoader;

	private Context context;

	public ProductCategoryAdapter(Context context,
			List<ProductCategory> productCategories) {
		this.context = context;
		this.productCategories = productCategories;
		imageLoader = new ImageLoader(context,context.getResources().getDrawable(testData[0]));
	}

	public void resetData(List<ProductCategory> productCategories) {
		this.productCategories = productCategories;
	}

	@Override
	public int getCount() {
		return productCategories.size();
	}

	@Override
	public ProductCategory getItem(int itemId) {
		return productCategories.get(itemId);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_category, null);
			holder = new ViewHolder();
			// holder.layout = (LinearLayout)
			// convertView.findViewById(R.id.base);
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.img_categoryicon);
			holder.textView = (TextView) convertView
					.findViewById(R.id.txt_categoryicon_label);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		ProductCategory productCategory = getItem(position);
		imageLoader.displayImage(productCategory.getProductCategoryIconUrl(),
				holder.imageView);
		holder.textView.setText(productCategory.getProductCategoryName());
		// holder.layout.setBackgroundResource(testData[position]);
		return convertView;
	}

	private final class ViewHolder {
		// LinearLayout layout;
		ImageView imageView;
		TextView textView;
	}

}
